<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class MemberModel extends Model
{
    protected $tablePrefix = '';
    protected $tableName = 'destoon_ucenter_member';

    /**
     * 获取当前用户信息,排除password和hash
     * @param $userid [用户id]
     * @return mixed
     */
    public function getMember($userid)
    {
        $map['userid'] = !empty($userid) ? $userid : "";
        $member = $this->where($map)->field('password,hash,bind_username', true)->select();
        return $member;
    }

    /**
     * 根据用户手机获取用户信息
     * @param $mobile
     * @return mixed
     * @throws Exception
     */
    public function getUserFromMobile($mobile)
    {
        $map['mobile'] = $mobile;
        $request = $this->where($map)->field('userid')->find();
        return $request;
    }

    /**
     * 更新密码
     * @param string $password 新密码
     * @param string $mobile 手机号
     * @return bool
     * @throws Exception
     */
    public function updatePassword($password, $mobile)
    {
        $data['password'] = $password;
        $map['mobile'] = $mobile;
        $result = $this->where($map)->save($data);
        if ($result === false) {
            throw new Exception(C('STATUS')['101']);
        } else {
            return $result;
        }
    }

    /**
     * 注册帐号
     * @param array $info 用户传入的数据
     * @return bool
     * @throws Exception
     */
    public function register($info)
    {
        $password = md5(md5($info['password']));
        $data = ['mobile' => $info['mobile'], 'password' => $password, 'addtime' => time(), 'updatetime' => time(), 'status' => 1, 'appid' => $info['appid'], 'last_login_time' => time()];
        $result = $this->add($data);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}